package com.example.shoppingmall.tool;

import com.example.shoppingmall.pojo.Coupon;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PoiUtils {
    /**
     * 导出
     * @param coupons
     * @return
     */
        public static ResponseEntity<byte[]> exportcoupon2Excel(Coupon coupons) {
            HttpHeaders headers = null;
            ByteArrayOutputStream baos = null;
            try {
                //1.创建Excel文档
                HSSFWorkbook workbook = new HSSFWorkbook();
                //2.创建文档摘要
                workbook.createInformationProperties();
                //3.获取文档信息，并配置
                DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation();
                //4.获取摘要信息并配置
                SummaryInformation si = workbook.getSummaryInformation();
                //4.1设置文档主题
                si.setSubject("优惠券");
                //4.2.设置文档标题
                si.setTitle("优惠券");
                //创建Excel表单
                HSSFSheet sheet = workbook.createSheet("优惠券");
                //创建日期显示格式
                HSSFCellStyle dateCellStyle = workbook.createCellStyle();
                dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
                //创建标题的显示样式
                HSSFCellStyle headerStyle = workbook.createCellStyle();
                headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
                headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                //定义列的宽度
                sheet.setColumnWidth(0, 10 * 256);
                sheet.setColumnWidth(1, 10 * 256);
                sheet.setColumnWidth(2, 10 * 256);
                sheet.setColumnWidth(3, 10 * 256);
                sheet.setColumnWidth(4, 10 * 256);

                //5.设置表头
                HSSFRow headerRow = sheet.createRow(0);
                HSSFCell cell0 = headerRow.createCell(0);
                cell0.setCellValue("优惠券名称");
                cell0.setCellStyle(headerStyle);
                HSSFCell cell1 = headerRow.createCell(1);
                cell1.setCellValue("权重");
                cell1.setCellStyle(headerStyle);
                HSSFCell cell2 = headerRow.createCell(2);
                cell2.setCellValue("启用状态");
                cell2.setCellStyle(headerStyle);
                HSSFCell cell3 = headerRow.createCell(3);
                cell3.setCellValue("开始时间");
                cell3.setCellStyle(headerStyle);
                HSSFCell cell4 = headerRow.createCell(4);
                cell4.setCellValue("结束时间");
                cell4.setCellStyle(headerStyle);
                   //6.装数据
                    HSSFRow row = sheet.createRow(1);
                    Coupon coupon = coupons;
                    row.createCell(0).setCellValue(coupon.getCouponName());
                    row.createCell(1).setCellValue(coupon.getWeight());
                    if(coupon.getStartType()==1){
                        row.createCell(2).setCellValue("启用状态:是");
                    }
                    if(coupon.getStartType()==2){
                        row.createCell(2).setCellValue("启用状态:否");
                    }
                    HSSFCell beginTime = row.createCell(3);
                    beginTime.setCellValue(coupon.getBeginTime());
                    beginTime.setCellStyle(dateCellStyle);
                    HSSFCell endTime = row.createCell(4);
                    endTime.setCellValue(coupon.getEndTime());
                    endTime.setCellStyle(dateCellStyle);

                headers = new HttpHeaders();
                headers.setContentDispositionFormData("attachment",
                        new String("优惠券.xls".getBytes("UTF-8"), "iso-8859-1"));
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                baos = new ByteArrayOutputStream();
                workbook.write(baos);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
        }

}
